package org.example;

import com.sun.org.apache.bcel.internal.generic.ACONST_NULL;
import lombok.extern.slf4j.Slf4j;

import java.sql.*;

@Slf4j
public class LocalJDBCTransactionApplicationn {
    public static void main(String[] args) throws SQLException {
        Connection connection = getConnection();
        PreparedStatement ps1 = connection.prepareStatement(getSql(-100, "SuperMan"));
//        connection.setAutoCommit(false);
        ps1.execute();

    }

    private static Connection getConnection() throws SQLException {
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://192.168.2.92:3306/dist_tran_course";
        String username = "root";
        String password = "123456";
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            log.error(e.getMessage());
        }
        return DriverManager.getConnection(url, username, password);
    }

    private static String getSql(int amount, String username) {
        String amountStr = amount > 0 ? "+" + amount : "" + amount;
        return "UPDATE t_user SET amount = amount" + amountStr + " WHERE username = '" + username + "'";
    }
}
